package com.tvein.sanguo.zone.controller
{
	//flash
	import flash.media.Sound;
	import flash.utils.ByteArray;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	//pureMVC
	import org.puremvc.as3.multicore.interfaces.ICommand;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.utilities.fabrication.patterns.command.SimpleFabricationCommand;
	//custom
	import org.audiofx.mp3.MP3FileReferenceLoader;
	import org.audiofx.mp3.MP3SoundEvent;
	import com.tvein.sanguo.zone.model.SystemProxy;
//
	public class ZoneMusic extends SimpleFabricationCommand implements ICommand
	{
		private var systemData:SystemProxy;
		
		private var playMode:int = 0;
		private var changeMode:Boolean = false;
		private var tmpVoice:Number;
		
		override public function execute(notification:INotification) : void
		{
			systemData = facade.retrieveProxy(SystemProxy.NAME) as SystemProxy;
			
			var musicName:String = notification.getBody()[0];
			var musicData:ByteArray = notification.getBody()[1];
			
			if (systemData.musicName == null)
			{
				playMode = 1;
			}
			else
			{
				if (systemData.musicName != musicName)
				{
					playMode = 2;
				}
			}
			
			if (playMode > 0)
			{
				systemData.musicName = musicName;
				
				var loader:MP3FileReferenceLoader = new MP3FileReferenceLoader();
				loader.addEventListener(MP3SoundEvent.COMPLETE, mp3LoaderCompleteHandler);
				loader.getSoundByteArray(musicData);
			}
			
			musicData.clear();
		}
		
		private function mp3LoaderCompleteHandler(evt:MP3SoundEvent):void
		{
			evt.target.removeEventListener(MP3SoundEvent.COMPLETE, mp3LoaderCompleteHandler);
			
			systemData.music = evt.sound;
			
			tmpVoice = systemData.musicVol * .1;
			
			switch(playMode)
			{
				case 1:
					systemData.transform.volume = 1;
					systemData.channel = systemData.music.play(0, int.MAX_VALUE, systemData.transform);
					break;
				case 2:
					var checkTimer:Timer = new Timer(300);
					checkTimer.addEventListener(TimerEvent.TIMER, _musicChange);
					checkTimer.start();
					break;
			}
		}
		
		private function _musicChange(evt:TimerEvent):void
		{
			if (systemData.musicMute == true)
			{
				systemData.transform.volume = 0;
				return;
			}
			
			if (!changeMode)
			{
				if (systemData.transform.volume > tmpVoice * .01)
				{
					systemData.transform.volume -= tmpVoice * .01;
				}
				else
				{
					changeMode = true;
					
					systemData.channel.stop();
					
					systemData.transform.volume = 0;
					systemData.channel = systemData.music.play(0, int.MAX_VALUE);
				}
			}
			else
			{
				if (systemData.transform.volume < (systemData.musicVol - tmpVoice) * .01)
				{
					systemData.transform.volume += tmpVoice * .01;
				}
				else
				{
					systemData.transform.volume = systemData.musicVol * .01;
					evt.target.stop();
					evt.target.removeEventListener(TimerEvent.TIMER, _musicChange);
				}
			}
			
			systemData.channel.soundTransform = systemData.transform;
		}
	}
}